package net.alteiar.images.filter;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;

import net.alteiar.Array2D;
import net.alteiar.images.BasicImage;
import net.alteiar.images.IDrawable;
import net.alteiar.images.metadata.IImageInfo;
import net.alteiar.transformation.NoTransformation;

public abstract class BasicFilter extends IFilter implements Observer {
	protected final BasicImage layer;
	protected IImageInfo info;

	public BasicFilter(IImageInfo infos) {
		this(infos, IDrawable.MEDIUM_QUALITY);
	}

	public BasicFilter(IImageInfo infos, int precision) {
		this.info = infos;

		BufferedImage img = new BufferedImage(info.getImageWidth(),
				info.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
		layer = new BasicImage(img, precision);
		infos.addObserver(this);
	}

	protected Array2D getImageArray2D() {
		return new Array2D(layer.getImagePixels(), info.getImageWidth());
	}

	protected void updateFilter() {
		int[] img = layer.getImagePixels();
		info.getValues(img, new NoTransformation());
	}

	@Override
	public void draw(Graphics2D g2, double zoomFactor) {
		layer.draw(g2, zoomFactor);
	}

	@Override
	public void update(Observable o, Object arg) {
		this.updateFilter();
		this.setChanged();
		this.notifyObservers();
	}

}
